package com.navinfo.platform.fault.utils.day;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.navinfo.location.mileage.util.JsonUtil;
import com.navinfo.platform.fault.dto.BasicDataDto;

import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Metadata;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"ResultOfMethodCallIgnored", "MismatchedQueryAndUpdateOfCollection"})
public class VehicleBasicDataSchema {

    public static StructType createSchema() {
        List<StructField> list = new ArrayList<>();
        list.add(new StructField("uid", DataTypes.LongType, true, Metadata.empty()));
        list.add(new StructField("tid", DataTypes.LongType, true, Metadata.empty()));

        List<StructField> aslStructFields = new ArrayList<>();
        for (int i = 0; i <= 13; i++) {
            StructField aslTime = DataTypes.createStructField("time", DataTypes.IntegerType, true, Metadata.empty());
            StructField aslMileage = DataTypes.createStructField("mileage", DataTypes.IntegerType, true, Metadata.empty());
            StructField aslFuel = DataTypes.createStructField("fuel", DataTypes.IntegerType, true, Metadata.empty());
            StructType aslInfo = DataTypes.createStructType(new StructField[]{aslTime, aslMileage, aslFuel});
            StructField aslField = DataTypes.createStructField("asl" + i, aslInfo, true, Metadata.empty());
            aslStructFields.add(aslField);
        }

        StructType altitudeInfo = DataTypes.createStructType(aslStructFields);

        StructField altitudeInfoField = DataTypes.createStructField("altitude_info", altitudeInfo, true, Metadata.empty());
        list.add(altitudeInfoField);

        List<StructField> hourStructFields = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            StructField hourState = DataTypes.createStructField("state", DataTypes.IntegerType, true, Metadata.empty());
            StructField hourTime = DataTypes.createStructField("time", DataTypes.IntegerType, true, Metadata.empty());
            StructField hourMileage = DataTypes.createStructField("mileage", DataTypes.IntegerType, true, Metadata.empty());
            StructField hourFuel = DataTypes.createStructField("fuel", DataTypes.IntegerType, true, Metadata.empty());
            StructType hour = DataTypes.createStructType(new StructField[]{hourState, hourTime, hourMileage, hourFuel});
            StructField hourField = DataTypes.createStructField("hour" + i + "", hour, true, Metadata.empty());
            hourStructFields.add(hourField);
        }

        StructType vehicleHourInfo = DataTypes.createStructType(hourStructFields);

        StructField vehicleHourInfoField = DataTypes.createStructField("vehicle_hour_info", vehicleHourInfo, true, Metadata.empty());
        list.add(vehicleHourInfoField);
        list.add(new StructField("update_time", DataTypes.TimestampType, true, Metadata.empty()));
        //add row start
        list.add(new StructField("sharp_turning_mil", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("so90_fuel", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("so120_fuel", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("fatigue_driving_duration", DataTypes.IntegerType, true, Metadata.empty()));


        //新增按行程累加的吨/公里字段字段
        list.add(new StructField("ton_km_info", DataTypes.StringType, true, Metadata.empty()));

        //新增按行程累加的经过城市详细信息的字段
        list.add(new StructField("cross_cities_info", DataTypes.StringType, true, Metadata.empty()));


        return new StructType(list.toArray(new StructField[list.size()]));
    }


    public static Integer getIntValue(Long value) {
        if (value == null) {
            return null;
        }
        return value.intValue();
    }

    public static Integer getIntValue(Integer value) {
        if (value == null) {
            return null;
        }
        return value;
    }

    public static Row createRow(BasicDataDto basicDataDto, StructType schema) {
        List<Object> list = new ArrayList<>();

        list.add(basicDataDto.getTid());




        return new GenericRowWithSchema(list.toArray(), schema);
    }

}
